package com.quanjiaolian.playground.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.quanjiaolian.playground.service.PlaygroundService;
import com.quanjiaolian.playground.serviceimpl.PlaygroundServiceImpl;
import com.quanjiaolian.tools.LocationUtils;
import com.quanjiaolian.tools.ResponseToJson;
import com.quanjiaolian.tools.Utils;

/**
 * 查询场馆列表
 * @author Herson-Li
 *
 */
@SuppressWarnings("serial")
public class FindAllPGInfo extends HttpServlet {

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(FindAllPGInfo.class.getName());
	
	Map<String,Object> resultMap = new HashMap<String,Object>();
	PlaygroundService pgs = new PlaygroundServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);

	}

	/**
	 * 查询场馆列表
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String firstSize = request.getParameter("firstSize");//起始页
		String maxSize = request.getParameter("maxSize");//返回数据条数
		String latitude = request.getParameter("latitude");//用户纬度
		String longitude = request.getParameter("longitude");//用户精度
		String pg_name = request.getParameter("pg_name");//查询场馆名称关键字
		String type = request.getParameter("type");//查询类型 0为查询全部场馆数据 1为查询离用户最近的场馆数据
		Double minLat = 0.0;//最小纬度范围
	    Double maxLat = 0.0;//最大纬度范围
	    Double minLng = 0.0;//最小精度范围    
        Double maxLng = 0.0;//最大精度范围
		if(firstSize == null || "0".equals(firstSize)){
			firstSize = "1";
		}
		if(maxSize == null){
			maxSize = "5";
		}
		if(type == null || "".equals(type)){
			resultMap.put("result","Error");
		}
		//如果type为0 则返回所有场馆数据
		if("0".equals(type)){
			try{
				List<Object> list = pgs.queryAllPGInfo(Integer.parseInt(firstSize),Integer.parseInt(maxSize),latitude,longitude,Utils.convertString(pg_name));
				if(list != null && list.size()>0){
					resultMap.put("result",list);
				}else{
					resultMap.put("result","no result");
				}
				
			}catch (Exception e) {
				resultMap.put("result","Error");
				logger.info(e);
//				e.printStackTrace();
			}
		}
		//如果type为1 则根据用户位置返回离用户最近的场馆数据
		if("1".equals(type)){
			try{
				double[] db = LocationUtils.getAround(Double.valueOf(latitude),Double.valueOf(longitude),5000);
				minLat = db[0];
				minLng = db[1];
				maxLat = db[2];
				maxLng = db[3];
				List<Object> list = pgs.queryNearByUser(Integer.parseInt(firstSize),Integer.parseInt(maxSize),latitude,longitude,minLat,minLng,maxLat,maxLng);
				if(list != null && list.size()>0){
					resultMap.put("result",list);
				}else{
					resultMap.put("result","no result");
				}
			}catch (Exception e) {
				resultMap.put("result","Error");
				logger.info(e);
//				e.printStackTrace();
			}
		}
		ResponseToJson.responseOutWithJson(response, resultMap);
		
	}

}
